package colin.project.sso.server.business.observer.handler;

import colin.project.sso.server.business.dao.SysAppDao;
import colin.project.sso.server.business.dao.SysPermissionDao;
import colin.project.sso.server.business.observer.enums.OperationEventEnums;
import com.sun.org.apache.xpath.internal.Arg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author joker
 * @Title PermissionOperationHandler
 * @Description
 * @Date 2019/5/18 5:15 PM
 * @Since
 * @Version 1.0.0
 */
@Component
public final class PermissionOperationHandler extends AbstractOperationHandler {

    @Autowired
    private SysPermissionDao permissionDao;

    //事件操作前缀
    private final static String PERMISSION_OPERATION_PREFIX = "Permission_4";


    /**
     * 是否接受对象
     *
     * @param operationEventEnums 检查的对象
     * @return 是否接受对象
     */
    @Override
    public boolean accept(OperationEventEnums operationEventEnums) {
        if (operationEventEnums.name().startsWith(PERMISSION_OPERATION_PREFIX)) {
            return true;
        }
        return false;
    }

    /**
     * 事件具体的处理
     *
     * @param event
     * @param source
     */
    @Override
    public void handle(Object source) {
        switch (super.getEvent()) {
            case Permission_4_DeleteByAppIds:
                if (null == source) {
                    throw new IllegalArgumentException("Permission_4_DeleteByAppIds目标方法入参id集合不能为空!");
                }
                Object[] args = (Object[]) source;
                handle4DeleteByAppIds((List<Integer>) args[0]);
                break;
            default:
        }
    }


    /**
     * 删除数据
     * @param ids
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    private void handle4DeleteByAppIds(List<Integer> ids) {
        Map<String, Object> params = new HashMap<>();
        params.put("ids", ids);
        permissionDao.getSQLManager().update("SysPermission.updateIdsForDelete", params);
    }
}
